<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\admin\model\Comment;
use app\common\model\Category as CategoryModel;
/**
 * 同城接口
 */
class Contribution extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $LitestoreGoods = null;
    protected $Manuscript = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        // $this->user_id = 1;
        $this->LitestoreGoods = new \app\admin\model\LitestoreGoods;
        $this->Manuscript = new \app\admin\model\Manuscript;
    }  

    /**
     * 发布同城和修改同城
     * @ApiSummary  (修改时传id过来)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Contribution/post)
     * @ApiParams   (name="id",type=int,required=true,description="修改时候传id过来")
     * @ApiParams   (name="images",type=string,required=false,description="多个图片的情况下用逗号隔开")
     * @ApiParams   (name="title",type=string,required=false,description="标题")
     * @ApiParams   (name="pid",type=int,required=false,description="同城分类id")
     * @ApiParams   (name="contact",type=string,required=false,description="联系方式")
     * @ApiParams   (name="price",type=decimal,required=false,description="价格")
     * @ApiParams   (name="content",type=string,required=false,description="描述详情")
     * @ApiReturn   ({
        
        })
     */
      public function post(){
        $params = input();
        $params['user_id'] = $this->user_id;
        $params['type'] = 'manuscript';

        if($params['id']){
            $res = $this->Manuscript->isUpdate(true)->save($params);
        }else{
            unset($params['id']);
            $res = $this->Manuscript->save($params);
        }
        if($res){
            $this->success('操作成功');
        }
      }
    /**
    * 同城分类列表 
    **/
    public function Category(){
      $return = Db::name('category')->where('type','Manuscript')->select();
      $this->success('同城分类列表',$return);
    }

    /**
    * 获取同城列表 
    **/
    public function index(){
      
      $return = $this->Manuscript->with('user')->where('status',1)->order('weigh desc')->select();
      $this->success('获取同城列表',$return);
    }

     /**
    * 搜索同城 
    * @param string $title 搜索名称
    **/
      public function search(){
        $title = input('title');
        $return = $this->Manuscript->with('user')->where(['title'=>['like',"%$title%"]])->order('weigh desc')->select();
    
        $this->success('搜索同城',$return);
      }


    /**
    * 获取同城详情 is_collect=1代表已收藏，0代表未收藏
    * @param int $id 同城id
    **/
      public function detail(){
        $return = $this->Manuscript->with('user')->where(['id'=>input('id')])->order('weigh desc')->find();
        $my_collect = Db::name('collect')->where(['user_id'=>$this->user_id])->value('collect');
        $my_collect = json_decode($my_collect,true);

        $key = array_search(input('id'), $my_collect);
        if ($key !== false){
          $return['is_collect'] = 1;
        }else{
          $return['is_collect'] = 0;
        }
        $this->success('获取投稿详情',$return);

      }
    
     /**
     * 收藏和取消收藏
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/contribution/collect)
     * @ApiParams   (name="id",type=int,required=false,description="id")
     * @ApiParams   (name="status",type=int,required=false,description="1是收藏，0是取消收藏")
     * @ApiReturn   ({
        })
     */
    public function collect(){
      $id = input('id');
      $status = input('status');

      $my_collect = Db::name('collect')->where(['user_id'=>$this->user_id])->value('collect');
      if($my_collect){
        $my_collect = json_decode($my_collect,true);
      }
      // 收藏
      if($status){
        if(!empty($my_collect)){
       
          if(in_array($id, $my_collect)){
            $this->error('请勿重复收藏');die;
          }

        }
        
        $my_collect[] = $id;
        $collect_json = json_encode($my_collect);

        Db::name('collect')->where(['user_id'=>$this->user_id])->update(['collect'=>$collect_json]);
        $this->success('收藏成功');
      }else{
        $key = array_search($id, $my_collect);

        if ($key !== false){
          unset($my_collect[$key]);
          $collect_json = json_encode($my_collect);

          Db::name('collect')->where(['user_id'=>$this->user_id])->update(['collect'=>$collect_json]);
          $this->success('取消收藏成功');
        }else{
          $this->error('取消收藏失败');
        }
      }


    }


    

  

}
